//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
//-----------------------------------------------------------------------------
// NET.H
//
//-----------------------------------------------------------------------------
#ifndef NET_NET_H
#define NET_NET_H


#define INTSWAP(n) ( ((n&0xff00)>>8) + ((n&0x00ff)<<8) )

// Event word bits
#define EVENT_ETH_ARRIVED			0x0001
#define EVENT_AGE_ARP_CACHE		    0x0002
#define EVENT_TCP_RETRANSMIT		0x0004
#define EVENT_TCP_INACTIVITY		0x0008
#define EVENT_ARP_RETRANSMIT		0x0010


// Type number field in Ethernet frame
#define IP_PACKET             	    INTSWAP(0x0800)
#define ARP_PACKET            	    INTSWAP(0x0806)
#define RARP_PACKET					INTSWAP(0x8035)

// Protocol identifier field in IP datagram
#define ICMP_TYPE             	        1
#define IGMP_TYPE						2
#define TCP_TYPE              	        6
#define UDP_TYPE              	        17

// Message type field in ARP messages
#define ARP_REQUEST                 INTSWAP(0x0001)
#define ARP_RESPONSE          	    INTSWAP(0x0002)
#define RARP_REQUEST		        INTSWAP(0x0003)
#define RARP_RESPONSE         	    INTSWAP(0x0004)

// Hardware type field in ARP message
#define DIX_ETHERNET          	    INTSWAP(0x0001)
#define IEEE_ETHERNET         	    INTSWAP(0x0006)



typedef struct
{
    UNS_32 ipaddr;
    UNS_8  hwaddr[6];
    UNS_8  timer;
} ARP_CACHE;


typedef struct
{
    UNS_8   *buf;
    UNS_32  ipaddr;
    UNS_8   proto_id;
    UNS_16  len;
    UNS_8   timer;
} WAIT;


typedef struct
{
    UNS_16  hardware_type;
    UNS_16  protocol_type;

    UNS_8   hwaddr_len;
    UNS_8   ipaddr_len;
    UNS_16  message_type;

    UNS_8   source_hwaddr[6];
    UNS_32  source_ipaddr;
    UNS_8   dest_hwaddr[6];
    UNS_32  dest_ipaddr;
} __attribute__ ((packed)) ARP_HEADER;
#define ARP_HEADER_LEN      28

typedef struct
{
    UNS_8   dest_hwaddr[6];
    UNS_8   source_hwaddr[6];
    UNS_16  frame_type;
} __attribute__ ((packed)) ETH_HEADER;
#define ETH_HEADER_LEN      14

typedef struct
{
    UNS_8   ver_len;
    UNS_8   type_of_service;
    UNS_16  total_length;

    UNS_16  identifier;
    UNS_16  fragment_info;

    UNS_8   time_to_live;
    UNS_8   protocol_id;
    UNS_16  header_cksum;

    UNS_32  source_ipaddr;

    UNS_32  dest_ipaddr;
}  __attribute__ ((packed)) IP_HEADER;
#define IP_HEADER_LEN        20

typedef struct
{
    UNS_8   msg_type;
    UNS_8   msg_code;
    UNS_16  checksum;

    UNS_16  identifier;
    UNS_16  sequence;

    UNS_8   echo_data;
} __attribute__ ((packed)) PING_HEADER;
#define PING_HEADER_LEN      8

// This is the format for the UDP packet header
typedef struct {
    UNS_16 source_port;
    UNS_16 dest_port;
    UNS_16 udp_length;
    UNS_16 udp_checksum;
    // Then comes the data
} __attribute__ ((packed)) UDP_HEADER;

typedef struct {
    UNS_32 source_ipaddr;
    UNS_32 dest_ipaddr;
    UNS_8 zero;
    UNS_8 protocol_id;
    UNS_16 total_length;
} __attribute__ ((packed)) UDP_PSEUDO_HEADER;

typedef struct {
    UNS_16 *pdata;
    UNS_16 length;
    UNS_16 dest_port;
    UNS_16 source_port;
} __attribute__ ((packed)) UDP_DATA;

#include "net_arp.h"
#include "net_cksum.h"
#include "net_icmp.h"
#include "net_ip.h"

#define DBGMSG(s)   //dputs(s)

#endif //NET_NET_H

